package com.asktgapp.user.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.ShareVO;
import com.asktgapp.modulebase.common.util.LogUtil;
import com.asktgapp.utils.UmengShareUtils;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShareFragment extends BaseFragment {
    private TextView wxshare;
    //    private TextView weibo;
    private TextView qq;
    private TextView penyouquan;
    private TextView qqzone;
    //    private TextView copy;
    private String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.asktgapp";
    private String pic = "";
    ClipboardManager mManager;
    private ClipData mClipData;
    private int type;
    private String id = "";
    private ShareVO shareVO;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_share, container, false);
        return super.onCreateView(inflater, rootView, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        wxshare = (TextView) rootView.findViewById(R.id.tv_wx_share);
        wxshare.setOnClickListener(this);
//        weibo = (TextView) rootView.findViewById(R.id.weibo);
//        weibo.setOnClickListener(this);
        type = getActivity().getIntent().getIntExtra("type", 0);
        id = getActivity().getIntent().getStringExtra("id");

        qq = (TextView) rootView.findViewById(R.id.qq);
        qq.setOnClickListener(this);
        penyouquan = (TextView) rootView.findViewById(R.id.penyouquan);
        penyouquan.setOnClickListener(this);
        qqzone = (TextView) rootView.findViewById(R.id.qqzone);
        qqzone.setOnClickListener(this);
//        copy = (TextView) rootView.findViewById(R.id.copy);
//        copy.setOnClickListener(this);
        getBaseActivity().setBackNavigationIcon();

    }

    @Override
    public void loadData() {
        if(type!=-1){
            visibleLoading();
            Apiservice apiservice = ApiUtil.getInstance().create();
            HashMap params = new HashMap();
            params.put("id", id);
            params.put("type", type);
            Call<ApiResponseBody<ShareVO>> call = apiservice.shareInfo(params);
            call.enqueue(new Callback<ApiResponseBody<ShareVO>>() {
                @Override
                public void onResponse(Call<ApiResponseBody<ShareVO>> call, Response<ApiResponseBody<ShareVO>> response) {
                    inVisibleLoading();
                    if (response.isSuccessful()) {
                        shareVO = response.body().getResult();

                    } else {
                        showTost(response.raw().message(), 1);
                    }
                }

                @Override
                public void onFailure(Call<ApiResponseBody<ShareVO>> call, Throwable t) {
                    inVisibleLoading();
                    ApiException apiException = ExceptionHandle.handleException(t);
                    if (apiException.isTokenInvalid()) {
                        showUnLoginSnackbar();
                    } else if (apiException.isNetConnected()) {
                        showSetNetworkSnackbar();
                    } else {
                        showTost(apiException.getMessage(), 0);
                    }

                }
            });
        }else {
            shareVO = new ShareVO();
            shareVO.setTitle("我在这里学到很多知识，邀请您加入剪工共同进步");
            shareVO.setContent("这里有很多专业人士，有很多二手车辆信息，点击链接进行下载，共同学习相关知识");
            shareVO.setUrl(url);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_wx_share:
                UmengShareUtils.socialShare(getActivity(), SHARE_MEDIA.WEIXIN, shareVO.getTitle(), shareVO.getContent(), shareVO.getUrl(), shareVO.getLogo(), new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "开始分享！", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        LogUtil.LogD("分享失败消息");
                        Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                    }
                });
                break;
            case R.id.qq:
                UmengShareUtils.socialShare(getActivity(), SHARE_MEDIA.QQ, shareVO.getTitle(), shareVO.getContent(), shareVO.getUrl(), shareVO.getLogo(), new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "开始分享！", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        LogUtil.LogD("分享失败消息");
                        Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
//                        Toast.makeText(getActivity(),"取消分享", Toast.LENGTH_LONG).show();
                    }
                });

                break;
            case R.id.penyouquan:
                UmengShareUtils.socialShare(getActivity(), SHARE_MEDIA.WEIXIN_CIRCLE, shareVO.getTitle(), shareVO.getContent(), shareVO.getUrl(), shareVO.getLogo(), new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "开始分享！", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        LogUtil.LogD("分享失败消息");
                        Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

//                        Toast.makeText(getActivity(),"取消分享", Toast.LENGTH_LONG).show();
                    }
                });
                break;
            case R.id.qqzone:
                UmengShareUtils.socialShare(getActivity(), SHARE_MEDIA.QZONE, shareVO.getTitle(), shareVO.getContent(), shareVO.getUrl(), shareVO.getLogo(), new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "开始分享！", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        LogUtil.LogD("分享失败消息");
                        Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

//                        Toast.makeText(getActivity(),"取消分享", Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
        super.onClick(v);
    }
}
